package org.aiforum.backend.service;

import java.util.List;

import javax.transaction.Transactional;
import javax.transaction.Transactional.TxType;

import org.aiforum.backend.common.QueryPreperty;
import org.aiforum.backend.common.Response;
import org.aiforum.backend.mapper.PlateMapper;
import org.aiforum.domain.Plate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.util.StringUtil;
/**
 * 板块业务实现
 * @author Administrator
 *
 */
@Service("plateService")
@Transactional(TxType.REQUIRED)
public class PlateServiceImpl implements PlateService{
    
	@Autowired
	private PlateMapper plateMapper;
	
	
	@Override
	public Response insert(Plate plate) {
		//初始化板块对象
		plate.setPlateState(1);
		plateMapper.insert(plate);
		
		//创建Response对象
		Response response=Response.getInstaner();
		response.setState(true);
		response.setMessage("Add success");
		return response;
	}

	@Override
	public Response update(Plate plate) {
		//初始化板块对象
		plate.setPlateState(1);
		plateMapper.update(plate);
		//创建Response对象
		Response response=Response.getInstaner();
		response.setState(true);
		response.setMessage("Update success");
		return response;
	}

	@Override
	public Plate find(int plateId) {
		Plate plate=plateMapper.findById(plateId);
		return plate;
	}

	@Override
	public Response delete(int plateId) {
		plateMapper.delete(plateId);
		Response response=Response.getInstaner();
		response.setState(true);
		response.setMessage("Delete Success");
		return response;
	}

	@Override
	public List<Plate> queryByPreperty(QueryPreperty queryPreperty) {
		//如果查询的属性值不为NULL或""
		if(StringUtil.isNotEmpty(queryPreperty.getQueryValue().toString())){
			//构建模糊查询条件
			String value="%"+queryPreperty.getQueryValue()+"%";
			queryPreperty.setQueryValue(value);
		}else{
			//否则设置为空
			queryPreperty.setQueryValue(null);
		}
		return plateMapper.queryByPreperty(queryPreperty);
	}

	@Override
	public Response checkExists(QueryPreperty queryPreperty) {
		//根据属性查询是否有记录
		int count=plateMapper.checkExists(queryPreperty);
		Response response=Response.getInstaner();
		if(count>0){
			response.setState(true);
			response.setMessage(queryPreperty.getQueryName()+"already exists");
		}
		return response;
	}

}
